*------------------------------------------------------------------------------------------------------
*
* SHOCK SERIES
*
*------------------------------------------------------------------------------------------------------

clear all

set more off


qui{

*_________ GET DATA ____________
use "data/final dataset/Vagaries.dta", clear
tsset year, yearly



*_________ SHOCK TABLE ____________
preserve
drop if year < 1531 | year > 1810
replace sinkmonth1 = 999 if year==1550 // exact month unknown for 1550 events; set to usual return voyages schedule for disaster location (Chaunu and Chaunu, 1977, pp.229ff.)
replace sinkmonth2 = 999 if year ==1550

gen unit = "kg"
forvalues k = 1/3 {
	format loss`k'  %9.0gc
	format salv`k'  %9.0gc
	replace loss`k' = round(loss`k',1)
	replace salv`k' = round(salv`k',1)
	
	egen loss`k'_st = concat(loss`k' unit), format(%9.0gc) punct(" ")
	egen salv`k'_st = concat(salv`k' unit), format(%9.0gc) punct(" ")
	
	replace loss`k'_st = "" if loss`k'==.
	replace salv`k'_st = "" if salv`k'==.
	replace salv`k'_st = "NA" if salv`k'==. & cause`k'=="capture"
	
	gen sm`k' = ""
	replace sm`k' = "Jan" if sinkmonth`k'==1
	replace sm`k' = "Feb" if sinkmonth`k'==2
	replace sm`k' = "Mar" if sinkmonth`k'==3
	replace sm`k' = "Apr" if sinkmonth`k'==4
	replace sm`k' = "May" if sinkmonth`k'==5
	replace sm`k' = "Jun" if sinkmonth`k'==6
	replace sm`k' = "Jul" if sinkmonth`k'==7
	replace sm`k' = "Aug" if sinkmonth`k'==8
	replace sm`k' = "Sep" if sinkmonth`k'==9
	replace sm`k' = "Oct" if sinkmonth`k'==10
	replace sm`k' = "Nov" if sinkmonth`k'==11
	replace sm`k' = "Dec" if sinkmonth`k'==12
	replace sm`k' = "NA" if sinkmonth`k'==999

	
	gen loc`k' = location`k'
	replace loc`k' = "Europe" if loc`k'=="E"
	replace loc`k' = "Azores" if loc`k'=="A"
	replace loc`k' = "Bermudas" if loc`k'=="B"
	replace loc`k' = "Havana" if loc`k'=="H"
	replace loc`k' = "Veracruz" if loc`k'=="V"
	replace loc`k' = "Cartagena" if loc`k'=="C"
	replace loc`k' = "Portobelo" if loc`k'=="P"
	replace loc`k' = "Lima" if loc`k'=="S"
	replace loc`k' = "Rio de plata" if loc`k'=="R"
	}


egen losses2 = concat(loss1_st loss2_st), punct(" \newline ")
egen losses3 = concat(loss1_st loss2_st loss3_st), punct(" \newline ") 
gen loss=loss1_st
replace loss = losses2 if loss1_st!="" & loss2_st!=""
replace loss = losses3 if loss1_st!="" & loss2_st!="" & loss3_st!=""

egen salvs2 = concat(salv1_st salv2_st), punct(" \newline ")
egen salvs3 = concat(salv1_st salv2_st salv3_st), punct(" \newline ") 
gen salv=salv1_st
replace salv = salvs2 if salv1_st!="" & salv2_st!=""
replace salv = salvs3 if salv1_st!="" & salv2_st!="" & salv3_st!=""

egen causes2 = concat(cause1 cause2), punct(" \newline ")
egen causes3 = concat(cause1 cause2 cause3), punct(" \newline ")
gen cause=cause1
replace cause = causes2 if cause1!="" & cause2!=""
replace cause = causes3 if cause1!="" & cause2!="" & cause3!=""

egen locations2 = concat(loc1 loc2), punct(" \newline ")
egen locations3 = concat(loc1 loc2 loc3), punct(" \newline ")
gen location=loc1
replace location = locations2 if loc1!="" & loc2!=""
replace location = locations3 if loc1!="" & loc2!="" & loc3!=""

egen sms2 = concat(sm1 sm2), punct(" \newline ")
egen sms3 = concat(sm1 sm2 sm3), punct(" \newline ")
gen sm=sm1
replace sm = sms2 if sm1!="" & sm2!=""
replace sm = sms3 if sm1!="" & sm2!="" & sm3!=""



gen disaster = 1 if loss1>0 & loss1!=.
replace disaster = 0 if disaster==.

keep if disaster==1

listtab year loss cause salv location sm  using "results/Table1.tex", replace ///
headlines( \begin{tabular}{l p{2cm} p{5cm} p{3cm} p{2.5cm} p{2cm}} \\ \hline \hline Year  & Silver loss & Cause of maritime disaster & Salvaged silver & Location & Month \\ \hline ) ///
footlines(\hline \hline \end{tabular}) ///
delimiter( & ) ///
end(" \\  " ) ///


restore




*_________ TIMING OF SHOCKS ____________
replace sinkmonth1 = 7 if year==1550 // exact month unknown for 1550 events; set to usual return voyages schedule for disaster location (Chaunu and Chaunu, 1977, pp.229ff.)
replace sinkmonth2 = 9 if year ==1550

//traveltimes from Chaunnu, Pierre. 1977. Seville et L'Amérique. pp.231ff.; except for South sea and Rio de Plata
forvalues k = 1/3 {
	gen traveltime`k'=.
	replace traveltime`k'=0 if location`k'=="E" //Europe 
	replace traveltime`k'=.5 if location`k'=="A" //Azores
	replace traveltime`k'=1.5 if location`k'=="B" //Bermudas 
	replace traveltime`k'= 4 if location`k'=="V" //Veracruz, Texas, Padre Island
	replace traveltime`k'=2 if location`k'=="H" //Havanna, Florida, Bahamas, Hispaniola, Dominica, Jamaica, Cuba, North Carolina, Virginia
	replace traveltime`k'=2.75 if location`k'=="C" //Cartagena 
	replace traveltime`k'=3 if location`k'=="P" //Portobelo, Panama
	replace traveltime`k'=4 if location`k'=="S" //Lima, South sea fleet 
	replace traveltime`k'=4 if location`k'=="R" //Rio de plata, Argentina
	
	//replace traveltime`k'=traveltime`k'+1 if traveltime`k'!=.
	}
	

forvalues k = 1/3 {
	gen shockarrival`k' = sinkmonth`k' + traveltime`k' //how many months after disaster news arrives in Spain
	gen shockarrival`k'_plus = sinkmonth`k' + traveltime`k' + 1 //robustness check: add one month

	gen org_loss`k' = loss`k'
	

	//baseline
	foreach v in loss salv conflict pir ships {
		gen `v'`k'_org = `v'`k'
		replace `v'`k'_org = 0 if `v'`k'_org == .
		}
	gen cause`k'_org = cause`k'	
		
	
	//plus
	foreach v in loss salv conflict pir ships {
		gen `v'`k'_plus = `v'`k'
		replace `v'`k'_plus = 0 if `v'`k'_plus == .
		}
		
	
	//baseline
	foreach v in loss salv conflict pir ships {
		gen shifted_`v'`k' = l.`v'`k'_org if l.shockarrival`k'>=12 & l.shockarrival`k'!=.
		gen nonshifted_`v'`k' = `v'`k'_org if shockarrival`k'<12
		replace shifted_`v'`k' = 0 if shifted_`v'`k'==.
		replace nonshifted_`v'`k' = 0 if nonshifted_`v'`k'==.
		replace `v'`k'_org = shifted_`v'`k' + nonshifted_`v'`k'
		drop shifted* nonshifted*
		}
	
	
	replace cause`k'_org = cause`k'_org[_n-1] if l.shockarrival`k'>=12 & l.shockarrival`k'!=.
	
	replace cause`k'_org = "" if shockarrival`k'>=12 & shockarrival`k'!=. 

	
	
	//plus
	foreach v in loss salv conflict pir ships {
		gen shifted_`v'`k' = l.`v'`k'_plus if l.shockarrival`k'_plus>=12 & l.shockarrival`k'_plus!=.
		gen nonshifted_`v'`k' = `v'`k'_plus if shockarrival`k'_plus<12
		replace shifted_`v'`k' = 0 if shifted_`v'`k'==.
		replace nonshifted_`v'`k' = 0 if nonshifted_`v'`k'==.
		replace `v'`k'_plus = shifted_`v'`k' + nonshifted_`v'`k'
		drop shifted* nonshifted*
		}
		
	
	}
		
	
forvalues k = 1/3 {
	replace org_loss`k' = 0 if org_loss`k'==.
	
	}
	
gen org_loss = 0
replace org_loss = org_loss+org_loss1 
replace org_loss = org_loss+org_loss2 
replace org_loss = org_loss+org_loss3 

local typp " "_org" "_plus"" 
foreach t of local typp {
	gen loss`t' = 0
	replace loss`t' = loss`t'+loss1`t' if pir1`t'==0
	replace loss`t' = loss`t'+loss2`t' if pir2`t'==0
	replace loss`t' = loss`t'+loss3`t' if pir3`t'==0

	gen loss_pir`t' = 0
	replace loss_pir`t' = loss_pir`t'+loss1`t' if pir1`t'==1 
	replace loss_pir`t' = loss_pir`t'+loss2`t' if pir2`t'==1
	replace loss_pir`t' = loss_pir`t'+loss3`t' if pir3`t'==1

	gen loss_noco`t' = 0
	replace loss_noco`t'  = loss_noco`t' +loss1`t' if conflict1`t'==0
	replace loss_noco`t'  = loss_noco`t' +loss2`t' if conflict2`t'==0
	replace loss_noco`t'  = loss_noco`t' +loss3`t' if conflict3`t'==0

	gen salv`t' = salv1`t' + salv2`t' + salv3`t'
	gen conflict`t' = conflict1`t' + conflict2`t' + conflict3`t'

	replace loss`t' = loss`t' + loss_pir`t'
	}
	
	gen loss = loss_org
	gen loss_pir = loss_pir_org
	gen loss_noco = loss_noco_org
	gen salv = salv_org
	gen conflict = conflict_org
	
	
	order org_loss* loss*
	
egen causes2 = concat(cause1 cause2), punct(", ")
egen causes3 = concat(cause1 cause2 cause3), punct(", ")
gen cause=cause1
replace cause = causes2 if cause1!="" & cause2!=""
replace cause = causes3 if cause1!="" & cause2!="" & cause3!=""
order cause



gen disaster_org = 1 if org_loss>0
replace disaster_org = 0 if disaster_org==.

gen disaster = 1 if loss>0 & loss!=.
replace disaster = 0 if disaster==.

gen pirate = 1 if loss_pir>0 & loss_pir!=.
replace pirate = 0 if pirate==.




*_________ ESP MONEY STOCK ____________
*_________ INTRA- & EXTRAPOLATE

forvalues x=1/50 {
replace gs_rate=f.gs_rate if gs_rate==. & f.gs_rate!=.
}





*_________ INFLOW TO SILVER STOCK RATIO

gen itsN = (in_esp - loss + l.salv)/(l.sisto_esp) * 100


n sum itsN, d

gen perclossN = loss/(l.sisto_esp)*100
replace perclossN = 0 if perclossN==. //fill up
gen perclossN_noco = loss_noco/(l.sisto_esp)*100
replace perclossN_noco = 0 if perclossN_noco==. //fill up

gen percloss_salvN = salv/l.sisto_esp*100
replace percloss_salvN = 0 if percloss_salvN==. //fill up

gen percloss_pirN = loss_pir/l.sisto_esp*100
replace percloss_pirN = 0 if percloss_pirN==. //fill up

n sum perclossN if perclossN > 0,d




gen itsN_plus = (in_esp - loss_plus + l.salv_plus)/(l.sisto_esp) * 100

n sum itsN_plus, d

gen perclossN_plus = loss_plus/(l.sisto_esp)*100
replace perclossN_plus = 0 if perclossN_plus==. //fill up
gen perclossN_noco_plus = loss_noco_plus/(l.sisto_esp)*100
replace perclossN_noco_plus = 0 if perclossN_noco_plus==. //fill up

gen percloss_salvN_plus = salv_plus/l.sisto_esp*100
replace percloss_salvN_plus = 0 if percloss_salvN_plus==. //fill up

gen percloss_pirN_plus = loss_pir_plus/l.sisto_esp*100
replace percloss_pirN_plus = 0 if percloss_pirN_plus==. //fill up




gen percloss_netN = perclossN - percloss_salvN
gen percloss_netN_noco = perclossN_noco - percloss_salvN
gen percloss_netN_plot = percloss_netN 
replace percloss_netN_plot = . if percloss_netN_plot==0

preserve

keep if year >=1531 & year <=1810
la var perclossN "Money loss (% of stock)"
la var percloss_netN_plot "Loss after salvage"

twoway (line perclossN year, mlabposition(3) lcolor(black) msize(tiny) ylabel(#3, angle(0) nogrid) xlabel(1531 1550(50)1750 1810) ytitle("")) ///
(scatter percloss_netN_plot year, msymbol(diamond) mcolor(gs7) msize(medsmall)), ///
xtitle("Year") plotregion(color(white)) graphregion(color(white)) name(shocks) ///
legend(order(1 2) col(2) region(lcolor(white))) 

graph display shocks, ysize(1.1) xsize(2)

graph export "results/Figure1.pdf", replace
restore


// Vellon period
gen vellon = 0
replace vellon = 1 if year >= 1617 & year <= 1664 // 1617 : the Cortes agreed to resumption of vellon minting; 1664 minting halted in october
gen perclossN_vellon = perclossN*vellon

gen perclossN_plus_vellon = perclossN_plus*vellon


*_________ STATS ____________
preserve

gen sisto_pesosN = sisto_esp*1000/pesograms // from kilograms of silver equivalent to pesos
gen in_esp_pesos = in_esp*1000/pesograms // from kilograms of silver equivalent to pesos
gen gdp_nom_pesos = gdp_nom_reales*10^6/20 // from reales de vellon to pesos (peso = 20 reales de vellon)
gen vN = gdp_nom_pesos/sisto_pesosN // velocity, inverse of silver to output ratio


gen inflow_to_GDP = in_esp_pesos/gdp_nom_pesos
sum inflow_to_GDP if year >=1531 & year <= 1810, d
n display `"Precious metal inflow to GDP ratio. mean: `r(mean)', min: `r(min)', max: `r(max)' "'
gen inflow_to_wealth_up = in_esp_pesos/(gdp_nom_pesos*5)
sum inflow_to_wealth_up if year >=1531 & year <= 1810, d
n display `"Precious metal inflow to wealth ratio, high. mean: `r(mean)', min: `r(min)', max: `r(max)' "'
gen inflow_to_wealth_lo = in_esp_pesos/(gdp_nom_pesos*7)
sum inflow_to_wealth_lo if year >=1531 & year <= 1810, d
n display `"Precious metal inflow to wealth ratio, low. mean: `r(mean)', min: `r(min)', max: `r(max)' "'
gen stockN_to_GDP =1/vN 
sum stockN_to_GDP if year >=1531 & year <= 1810, d
n display `"Money stock to GDP ratio. mean: `r(mean)', min: `r(min)', max: `r(max)' "'

restore




saveold "data/working files/VagariesMoney.dta", replace

} //quiet

